function bciPlotVariances(ecog,bci,ratioThresh)
 % bciPlotVariances(ecog,bci,ratioThresh)
 % INPUT: ecog struct, bci struct, threshold of maximum ratio (optional)
 % shows an color coded map of channels vs frequencies
 % the color codes the ratio of standard deviation (over time) to average standard
 % deviation (over channels)
 % potential noisy channels will be depicted by white text. 
 % first chanID: according to preprocChan (e.g.grid channel)
 % 2nd chanID (in paranthesis): according to position in buffer
 % The title of the figure gives an estimate of the artifact threshold
 % see also: bciRecordCalib, bciRawdata2Ecog
 
if nargin<3,
    ratioThresh = 4;
end

% Make Segments
windowLength=bci.init.nWinLength; % that's in samples
windowOnsetIdx=1:bci.init.nWinShift:ecog.nSamp-windowLength; 
[ecogSeg]=ecogSegmentTS(ecog,windowOnsetIdx,0,windowLength);

% Baseline Correction
ecogSeg.nBaselineSamp=size(ecogSeg.data,2);
ecogSeg=ecogBaselineCorrect(ecogSeg);

% stddev ratio of timeseries
sTS=std(ecog.data(ecog.selectedChannels,:),[],2); % stddev of each chan 
sTSRatio = sTS./mean(sTS);
bad_chan = find(sTSRatio>ratioThresh | sTSRatio==0);


% Periodogram
params.Fs=1000/ecog.sampDur;
params.fpass=[1 200];
params.tapers=bci.param.tapers;
ecogSeg=ecogMkPeriodogramMultitaper(ecogSeg,1,params);
P = permute(ecogSeg.periodogram.periodogram,[2 1 3]);

% % % find noisy channels using mean
% mSig = squeeze(mean(abs(P(ecog.selectedChannels,:,:)),3)); % mean of each chan and freq
% mSigRatio = mSig./repmat(mean(mSig,1),size(mSig,1),1); % ratio
% bad_chan = union(bad_chan,find(max(mSigRatio,[],2)>ratioThresh | sum(mSigRatio,2)==0)); % if any ratio of mean values exceeds 4, assume noise

% find noisy channels using std
sSig=squeeze(std(P(ecog.selectedChannels,:,:),[],3)); % stddev of each chan and freq
sSigRatio = sSig./repmat(mean(sSig,1),size(sSig,1),1); % ratio to mean over channels
bad_chan = union(bad_chan,find(max(sSigRatio,[],2)>ratioThresh | sum(sSigRatio,2)==0)); % if any ratio of mean values exceeds 4, assume noise

good_chan=setdiff(1:size(sSig,1),bad_chan);

figure;

imagesc([sTSRatio,sSigRatio]);
xTL={int2str(round(ecogSeg.periodogram.centerFrequency(str2num(get(gca,'xticklabel'))-1))')};
xTL=cat(2,{'TS'},xTL);
xT=([1,get(gca,'xtick')]);
set(gca,'xTick',xT,'xticklabel',xTL);
xlabel('Frequency [Hz]');
ylabel('Channel #');
for k=1:length(bad_chan),
    tH=text(1,bad_chan(k),sprintf('%i (%i)',bad_chan(k),ecog.selectedChannels(bad_chan(k))));
    set(tH,'fontweight','bold','color','w');
end
colorbar;
title(sprintf(['Ratio of StdDev to avgStdDev\n'...
    'Average TS StdDev of good Chans:\n'...
    '%0.5g (%0.5g - %0.5g) \n'...
    'ArtifactThresh %0.5g recommended 6*mean(std)'],...
    mean(sTS(good_chan)),min(sTS(good_chan)),max(sTS(good_chan)),6*mean(sTS(good_chan))));